﻿using System;
using System.Collections.Generic;
using System.Text;
using Navigant.Infrastructure.DomainBase;
using Navigant.Infrastructure;
using Navigant.Infrastructure.RepositoryFramework;
using Navigant.Infrastructure.Cases;

namespace Navigant.Model.PreservationNotices
{
    public class PreservationNoticeBatchService : Service<PreservationNoticeBatch>
    {
        private IUnitOfWork unitOfWork;
        private ICase currentCase;

        public PreservationNoticeBatchService(ICase currentCase)
            : this(currentCase, null)
        {
        }

        public PreservationNoticeBatchService(ICase currentCase, IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            this.currentCase = currentCase;
            this.unitOfWork = unitOfWork;
        }

        protected override IRepository<PreservationNoticeBatch> GetRepository()
        {
            return RepositoryFactory.GetRepository<IPreservationNoticeBatchRepository, 
                PreservationNoticeBatch>(this.currentCase, this.unitOfWork, false);
        }

        public PreservationNoticeBatch GetNextBatch()
        {
            return ((IPreservationNoticeBatchRepository)this.Repository).FindNext();
        }

        public void AddPreservationNoticeBatch(PreservationNoticeBatch batch)
        {
            this.Repository.Add(batch);
        }

        public void RemovePreservationNoticeBatch(PreservationNoticeBatch batch)
        {
            this.Repository.Remove(batch);
        }

        public void LogBatchResult(PreservationNoticeBatch batch,
            PreservationNoticeBatchProcessStatus status, string errorMessage)
        {
            ((IPreservationNoticeBatchRepository)this.Repository).AddToBatchHistory(batch,
                status, errorMessage);
        }

        public void RemoveBatches(IList<PreservationNoticeBatch> batchesToRemove)
        {
            foreach (PreservationNoticeBatch batch in batchesToRemove)
            {
                this.RemovePreservationNoticeBatch(batch);
            }
        }
    }
}
